<template>
  <div class='container'>
    <!-- Form -->
    <el-dialog :title="formData.id?'修改文章':'新增文章'" :visible="addDialogFormVisible" @close="close">
      <el-form ref="addRef" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="formData.title" placeholder="请输入文章标题"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <!-- 富文本编译器 -->
          <quill-editor v-model="formData.articleBody"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL">
          <el-input v-model="formData.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:addDialogFormVisible')">取 消</el-button>
        <el-button type="primary" @click="addArt">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'
export default {
  props: {
    addDialogFormVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 表单数据
      formData: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 表单验证
      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: ['blur', 'change'] }
        ],
        articleBody: [
          { required: true, message: '用文章内容不能为空', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 对话框关闭
    close () {
      this.$refs.addRef.resetFields()
      this.formData = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$emit('update:addDialogFormVisible')
    },
    // 添加/修改文章
    async addArt () {
      try {
        this.$refs.addRef.validate()
        if (this.formData.id) {
          await update(this.formData)
        } else {
          await add(this.formData)
        }
        this.$message.success('操作成功!')
        this.$emit('update:addDialogFormVisible')
        this.$parent.$parent.getlist()
        // console.log(this.$parent.$parent)
      } catch (error) {
        console.log(error)
      }
    },
    // 修改文章
    async updateArt (row) {
      this.formData.title = row.title
      this.formData.articleBody = row.articleBody
      this.formData.videoURL = row.videoURL
      this.formData.id = row.id
    }
  }
}
</script>

<style lang="scss" scoped>
// 设置富文本编辑器的默认最小高度
::v-deep .ql-editor {
  min-height: 200px;
}
</style>
